<template>
  <div>
    <div>{{city}}</div>
    <div>{{province}}</div>
  </div>
</template>

<script>
  import myBMap from './position.js'       //上面定义的js文件
  export default {
  data(){
      return{
          city:'',
          province:''
      }
  },
  methods: {
      getCity(){
        myBMap.init().then(BMap=>{
          const geolocation = new BMap.Geolocation()
          geolocation.getCurrentPosition((position)=>{
            this.city = position.address.city;             //获取城市信息
            this.province = position.address.province;     //获取省份信息
          },(e)=>{
            this.$store.commit('setCity',this.city)
            this.$store.commit('setProvince',this.province)
            console.log(e)
            console.log('定位失败')
          }, {provider: 'baidu'})
        })
      },
    },
    mounted () {
      this.getCity()      //调用函数获取城市信息
    }
  }
</script>
